package com.transsion.clouddemo.core

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout.GONE
import android.widget.LinearLayout.VISIBLE
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.transsion.clouddemo.CloudApplication
import com.transsion.clouddemo.CloudApplication.Companion.context
import com.transsion.clouddemo.R
import com.transsion.clouddemo.core.Custom.BaseActivity
import com.transsion.clouddemo.core.Custom.CloudClickListener
import com.transsion.clouddemo.core.Custom.TransIDManager.clearUserInfo
import com.transsion.clouddemo.core.config.ConfigPagerAdapter
import com.transsion.clouddemo.core.know.KnowPagerAdapter
import com.transsion.clouddemo.core.ulo.TryPagerAdapter
import com.transsion.clouddemo.databinding.CloudMainBinding
import com.transsion.lib_http.utilcode.util.LogUtils
import com.transsion.lib_interface.ServiceFetcher


class CloudHomeActivity : BaseActivity<CloudMainBinding>() {

    private lateinit var mTabLayout: TabLayout
    private var mLastIndex: Int = 0
    private lateinit var toolbar: Toolbar

    private var mInflateViews = arrayOfNulls<View>(3)

    private val mPagerAdapters =
        arrayOf(
            KnowPagerAdapter(this),
            TryPagerAdapter(this),
            ConfigPagerAdapter(this)
        )

    private lateinit var mTitles: Array<TextView>
    private lateinit var mLines: Array<View>

    companion object {
        private const val TOTAL_COUNT = 3

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }


    @SuppressLint("InflateParams")
    override fun init() {
        //初始化自定义标题
        toolbar = mBinding.toolbar
        setSupportActionBar(toolbar)
        // 隐藏默认标题
        supportActionBar?.setDisplayShowTitleEnabled(false)
        toolbar = mBinding.toolbar


        mTabLayout = mBinding.bottomTabLayout
        mBinding.title3.visibility = GONE
        mTitles = arrayOf(mBinding.title1, mBinding.title2, mBinding.title3)
        mLines = arrayOf(mBinding.line1, mBinding.line2, mBinding.line3)
        //初始化ViewPager2
        mBinding.homeViewPager.adapter = mPagerAdapters[0]

        //底部选项栏设置选择监听器
        mTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {

                //如果点击当前项则不作处理
                if (mLastIndex == tab.position) {
                    return
                }

                mBinding.homeViewPager.adapter = mPagerAdapters[tab.position]


                //设置对应的选中效果
                mInflateViews[tab.position]?.let {
                    DataGenerator.setTabView(
                        it,
                        tab.position,
                        true
                    )
                }

                //取消上个fragment的选中效果
                mInflateViews[mLastIndex]?.let { DataGenerator.setTabView(it, mLastIndex, false) }
                mLastIndex = tab.position


                //更换标题
                toolbar.post {
                    mBinding.toolbarTitle.text = getString(DataGenerator.mTabTitle[tab.position])
                }

                //初始化小标题
                when (tab.position) {
                    0 -> {
                        mBinding.title1.visibility= VISIBLE
                        mBinding.title2.visibility= VISIBLE
                        mBinding.title3.visibility= GONE
                        mBinding.line1.visibility = VISIBLE
                        mBinding.line2.visibility = GONE
                        mBinding.line3.visibility = GONE

                        mBinding.title1.text = context.getString(R.string.cloud_sync)
                        mBinding.title1.setTextColor(context.getColor(R.color.cloud_text_selected))
                        mBinding.title2.text = context.getString(R.string.cloud_access_guide)
                        mBinding.title2.setTextColor(context.getColor(R.color.cloud_text_default))

                    }

                    1 -> {
                        mBinding.title1.visibility= VISIBLE
                        mBinding.title2.visibility= VISIBLE
                        mBinding.title3.visibility= VISIBLE
                        mBinding.line1.visibility = VISIBLE
                        mBinding.line2.visibility = GONE
                        mBinding.line3.visibility = GONE


                        mBinding.title1.text = context.getString(R.string.cloud_down)
                        mBinding.title1.setTextColor(context.getColor(R.color.cloud_text_selected))
                        mBinding.title2.text = context.getString(R.string.cloud_upload)
                        mBinding.title2.setTextColor(context.getColor(R.color.cloud_text_default))
                        mBinding.title3.text = context.getString(R.string.cloud_other)
                        mBinding.title3.setTextColor(context.getColor(R.color.cloud_text_default))
                    }

                    2 -> {
                        mBinding.title1.visibility= VISIBLE
                        mBinding.title2.visibility= GONE
                        mBinding.title3.visibility= GONE
                        mBinding.line1.visibility = VISIBLE
                        mBinding.line2.visibility = GONE
                        mBinding.line3.visibility = GONE

                        mBinding.title1.text = context.getString(R.string.cloud_config)
                        mBinding.title1.setTextColor(context.getColor(R.color.cloud_text_selected))
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
            }

            override fun onTabReselected(tab: TabLayout.Tab) {

            }
        })

        //初始化TabLayout底部视图
        for (i in 0 until TOTAL_COUNT) {

            DataGenerator.getTabView(applicationContext, i).let {
                mTabLayout.addTab(
                    mTabLayout.newTab().setCustomView(it)
                )
                mInflateViews[i] = it
            }

        }

        //对小标题设置监听
        mBinding.title1.setOnClickListener(object : CloudClickListener() {
            override fun onSingleClick(view: View) {

                if (mBinding.line1.visibility == VISIBLE) {
                    return
                }
                mBinding.homeViewPager.currentItem = 0
                setValidColor(0)

            }

        })

        mBinding.title2.setOnClickListener(object : CloudClickListener() {
            override fun onSingleClick(view: View) {

                if (mBinding.line2.visibility == VISIBLE) {
                    return
                }
                mBinding.homeViewPager.currentItem = 1
                setValidColor(1)

            }
        })

        mBinding.title3.setOnClickListener(object : CloudClickListener() {
            override fun onSingleClick(view: View) {

                if (mBinding.line3.visibility == VISIBLE) {
                    return
                }
                mBinding.homeViewPager.currentItem = 2
                setValidColor(2)
            }

        })

        // 注册页面变化回调
        mBinding.homeViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                // 根据当前页面索引更新TextView内容
                setValidColor(position)
            }
        })

    }

    private fun setValidColor(index: Int) {
        for (i in 0 until 3) {
            if (i == index) {
                mTitles[i].setTextColor(context.getColor(R.color.cloud_text_selected))
                mLines[i].visibility = VISIBLE
                continue
            }
            mTitles[i].setTextColor(context.getColor(R.color.cloud_text_default))
            mLines[i].visibility = GONE
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        clearUserInfo()
    }
}